package de.rac.vaadin.framework.ui;
/*******************************************************************************
 * Management of all settings and translations and keeping references to important
 * general classes such as a class for getting a container by name and a reference to the main application<br>
 * @author Volodymyr Dobryvechir (dobrivecher@yahoo.com)
 * @version 0.2 31.05.2012  <br>
 ******************************************************************************/
import java.util.*;
import java.io.*;
import com.vaadin.Application;
import com.oros.db.*;
public class ResourcePool implements Serializable,AETranslation {
	/**
	 * 
	 */
	public boolean debug = true;
	private static final long serialVersionUID = -3236546083225447116L;
	private LinkedHashMap<String,String> config,commonLanguage,specificLanguage,design;
	private LinkedHashMap<String,LinkedHashMap<String,String>> specificDictionaryCache=
			new LinkedHashMap<String,LinkedHashMap<String,String>>();
	private Locale locale;
	private String specificLanguageName=null,commonLanguageName,specificLanguagePrefix;
	private Application application;
    private DatabasePool dbHelp;	
    public LanguagePool languagePool;
    private String userTable,userTableLoginColumn,userTablePasswordColumn,userTableEmailColumn,
           userTableLanguageColumn;
	public LinkedHashMap<String,String> readResource(String resourceName,Locale locale)
	{
        ResourceBundle resource = ResourceBundle.getBundle(resourceName,locale);
        Set<String> resSet = resource.keySet();
        LinkedHashMap<String,String> map = new LinkedHashMap<String,String>(resSet.size());
		for(String key:resource.keySet())
		{
			String value = resource.getString(key);
			map.put(key, value);
			//System.out.println(key+"="+value);
		}
	    return map;
	}
	public ResourcePool()
	{
		languagePool = new LanguagePool();
	}
    public ResourcePool(Locale locale,Application application,String configPath,String designPath,String languagePath,
    		String specLanguagePrefix)
    {
    	this.locale = locale;
    	languagePool = new LanguagePool();
    	if (configPath!=null) config = readResource(configPath,locale);
    	else config=new LinkedHashMap<String,String>();
    	if (designPath!=null) design = readResource(designPath,locale);
    	else design = new LinkedHashMap<String,String>();
    	commonLanguage = readResource(languagePath,locale);
    	commonLanguageName = languagePath;
    	specificLanguagePrefix = specLanguagePrefix;
        this.application = application;   	
    }
    public Application getApplication() { return application; }
    public Locale getLocale() { return locale;}
    
    public boolean changeLocale(Locale locale)
    {
    	if (!locale.equals(this.locale))
    	{
    	  this.locale = locale;
    	  specificDictionaryCache.clear();
    	  commonLanguage = readResource(commonLanguageName,locale);
    	  if (specificLanguageName!=null)
    	  {
    		  specificLanguage = readResource(specificLanguageName,locale);
		      specificDictionaryCache.put(specificLanguageName, specificLanguage);
    	  }
    	  return true;
    	}
    	return false;
    }
    public String getConfigParameter(String key)
    {
    	String val = config.get(key);
    	if (val==null) throw new ResourcePoolException("Undefined parameter of config: "+key);
    	return val;
    }
    public String getConfigParameterDirectly(String key)
    {
    	return config.get(key);
    }
    public void setConfigParameter(String key,String value)
    {
    	config.put(key, value);
    	if (debug) System.out.println("config["+key+"="+value+"]");
    }
    public void setSpecificLanguageResource(String resourceName)
    {
    	if (resourceName==null)
    	{
    		specificLanguageName=null;
    		return;
    	}
        resourceName = specificLanguagePrefix +resourceName;   	
    	if (specificLanguageName==null || !specificLanguageName.equals(resourceName))
    	{
    		specificLanguageName = resourceName;
    		specificLanguage = specificDictionaryCache.get(specificLanguageName);
    		if (specificLanguage==null)
    		{
  		       specificLanguage = readResource(specificLanguageName,locale);
  		       specificDictionaryCache.put(specificLanguageName, specificLanguage);
    		}
   	    }
    }
    public void setSpecificLanguageData(String key,String value)
    {
    	if (specificLanguageName==null) specificLanguageName="_default_dictionary_";
    	if (specificLanguage==null) specificLanguage=new LinkedHashMap<String,String>();
    	specificLanguage.put(key, value);
    }
    @Override
    public String getTranslationSoft(String key)
    {
    	if (specificLanguageName!=null)
    	{
    		String val=specificLanguage.get(key);
    		if (val!=null) return val;
    	}
    	String val = commonLanguage.get(key);
        return val;    	
    }    
    public String getTranslation(String key)
    {
    	String val=getTranslationSoft(key);
    	if (val==null)
    	{
    		throw new ResourcePoolException("Undefined translation for "+key);
    	}
    	return val;
    }
    public String[] getTranslationArray(String[] keys)
    {
    	int n=keys==null?0:keys.length;
    	String[] res = new String[n];
    	for(int i=0;i<n;i++)
    	{
    		res[i]=getTranslation(keys[i]);
    	}
    	return res;
    }
    public String getDesignDefinition(String key,String defaultVal)
    {
    	String val = design.get(key);
    	if (val==null) val=defaultVal;
    	return val;
    }
    public int getIntDesignDefinition(String key,int defaultVal)
    {
    	String val = design.get(key);
    	if (val==null) return defaultVal;
    	try
    	{
    		defaultVal = Integer.parseInt(val);
    	}
    	catch(Exception e)
    	{
    		System.out.println("Invalid number "+val+" in design definition "+key);
    	}
    	return defaultVal;
    }
    public void setDesignDefinition(String key,String value)
    {
    	design.put(key, value);
    	if (debug) System.out.println("design["+key+"="+value+"]");
    }
    public void setDatabasePool(DatabasePool dbHelp)
    {
    	this.dbHelp = dbHelp;
    }
    public DatabasePool getDatabasePool()
    {
    	return this.dbHelp;
    }
    public static String designHyphenKeyReplacement(String key)
    {
    	if (key==null) return key;
    	StringBuilder newKey = new StringBuilder();
    	int n=key.length();
    	for(int i=0;i<n;i++)
    	{
    		char c=key.charAt(i);
    		if (c=='-') c='_';
    		newKey.append(c);
    	}
    	return newKey.toString();
    }
	public String getUserTable() {
		return userTable;
	}
	public void setUserTable(String userTable) {
		if (userTable!=null)
		{
			userTable = userTable.trim();
			if (userTable.length()==0) userTable=null;
		}
		this.userTable = userTable;
	}
	public String getUserTableLoginColumn() {
		return userTableLoginColumn;
	}
	public void setUserTableLoginColumn(String userTableLoginColumn) {
		if (userTableLoginColumn!=null)
		{
			userTableLoginColumn = userTableLoginColumn.trim();
			if (userTableLoginColumn.length()==0) userTableLoginColumn = null;
		}
		this.userTableLoginColumn = userTableLoginColumn;
	}
	public String getUserTablePasswordColumn() {
		return userTablePasswordColumn;
	}
	public void setUserTablePasswordColumn(String userTablePasswordColumn) {
		if (userTablePasswordColumn!=null)
		{
			userTablePasswordColumn = userTablePasswordColumn.trim();
			if (userTablePasswordColumn.length()==0) userTablePasswordColumn=null; 
		}
		this.userTablePasswordColumn = userTablePasswordColumn;
	}
	public String getUserTableEmailColumn() {
		return userTableEmailColumn;
	}
	public void setUserTableEmailColumn(String userTableEmailColumn) {
		if (userTableEmailColumn!=null)
		{
			userTableEmailColumn = userTableEmailColumn.trim();
			if (userTableEmailColumn.length()==0) userTableEmailColumn = null;
		}
		this.userTableEmailColumn = userTableEmailColumn;
	}
	public String getUserTableLanguageColumn() {
		return userTableLanguageColumn;
	}
	public void setUserTableLanguageColumn(String userTableLanguageColumn) {
		if (userTableLanguageColumn!=null)
		{
			userTableLanguageColumn = userTableLanguageColumn.trim();
			if (userTableLanguageColumn.length()==0) userTableLanguageColumn = null;
		}
		this.userTableLanguageColumn = userTableLanguageColumn;
	}
}
